import CommonAction from '../../../public/actions/employing/common';
import Constant from './constant';
import ProcessTmp from './processTemplate';

import {getBusinessInfo,pageTo} from 'nc-lightapp-front';
import {getAppPageConfig} from '../../../../hrpub/common/utils/utils';

let businessInfo = getBusinessInfo() || {};

export default class MainAction extends CommonAction{
    constructor(comp) {
        super();
        this.comp = comp;
        if(comp.props.use){
            comp.props.use.form('card','entryinfo', 'psninf','tailinfo','auditinfo');
            comp.props.use.editTable('list','psninfo', 'org', 'dept');
            comp.props.use.search('querybill');
            comp.props.use.search('multiadd');
        }
    }

    extend = [ProcessTmp, Constant]

    appConfig = getAppPageConfig();

    // 处理模版
    processTemplate = (res) => {
        return this.selfProcessTemplate(res);
    }

    // 获取多语
    getCurrentLanguage = () => {
        this.getLanguage('gx6008', 'hrtrn')
            .then((json,status, init) => {
                this.update({
                    language: json
                })
                this.getTemplate();
            });
    }

    // 获取模版信息
    getTemplate = () => {
        this.getMultiTemplate(this.showReqData(this.appConfig))
            .then((res) => {
                this.update({
                    context: res.context,
                    showOrgRefer: true
                });
            });
    }

    // 当模版获取并设置完成之后的回调函数
    afterGetTemplate = () => {
        this.routePage();
    }
    widget = () =>{
        const {action} = this.comp;
        action.btnAct.changeOrg('widget')
    }
    // 初始化
    didMount = () => {
        // if (window.location.href.match('localhost:3006'))  window.location.hash = '#/ifr?page=2019'
        this.getCurrentLanguage();
        this.comp.action.btnAct.initPageButton();
        this.subscribe();
        this.getMainTableHeight();
        let type = pageTo.getUrlParam('type')
        if(type){
            setTimeout(() => {
                this.widget()
            }, 200);
        }
    }

    //  获取内容页面高度，并且设置表格高度
    getMainTableHeight = () => {
        if(window.getComputedStyle) {
            let app = document.getElementById('app');
            let height = window.getComputedStyle(app).height.replace('px', '');

            this.update({
                pageHeight: height
            });
        }
    }

    // 根据url判断是进入到编辑页面还是进入到主页面表格
    routePage = async () => {
        const {props, action} = this.comp;
        const {getUrlParam} = props;

        let scene = getUrlParam('scene');
        //从流程处理中心跳转的情况
        if(!scene && getUrlParam('status')){
            scene = 'approvesce'
        }
        let id = getUrlParam('id');
        let status = getUrlParam('status');

        let selectedRows = [{
            values: {
                pk_entryapply: {
                    value: id
                }
            }
        }];

        if(scene !== 'approvesce') {
            this.update({
                page: 'main'
            });
            return;
        }

        await this.update({
            orgValue:  {
                refpk: businessInfo.groupId
            },
            fromApprove: true,
            page: 'edit'
        });
        let methodsName = status === 'browse' ? 'toBrowsePage' : 'toEditPage'
        
        action.formAct[methodsName](selectedRows);
    }

    // 卸载
    willUnMount = () => {
        this.pubSub.unSubscribe('getMainTableData');
    }

    // 事件订阅部分
    subscribe = () => {
        const {comp: {action}, pubSub} = this;
        pubSub.subscribe('getMainTableData', (...args) => {
            action.tableAct.getMainTableData(...args);
        });
        pubSub.subscribe('getWidgetTableData', (...args) => {
            action.tableAct.getWidgetTableData(...args);
        });
    }
}